package com.tbit.main.common;

import com.tbit.main.constant.ResponseCode;

import java.io.Serializable;

/**
 * Json响应
 *
 * @author Leon
 * 2019年1月23日 下午3:49:22
 */
public class JsonResult<T> implements Serializable {
    /**
     * 分页记录总数
     */
    public Integer total;
    /**
     * 结果
     */
    private Integer ret;
    /**
     * 错误内容
     */
    private String content;
    /**
     * 错误消息
     */
    private String msg;
    /**
     * 结果
     */
    private T data;
    /**
     * 链路追踪traceId
     */
    private String traceId;

    /**
     * 额外的留给另外一个数量的字段
     * */
    private Integer num;

    private JsonResult(Integer ret) {
        this.ret = ret;
    }

    private JsonResult(T data, Integer ret) {
        this.data = data;
        this.ret = ret;
    }


    public JsonResult(Integer ret, String msg, String traceId) {
        super();
        this.ret = ret;
        this.msg = msg;
        this.traceId = traceId;
    }

    public JsonResult(Integer ret, String msg, String content, String traceId) {
        super();
        this.ret = ret;
        this.msg = msg;
        this.content = content;
        this.traceId = traceId;
    }

    public JsonResult(Integer ret, T data, Integer total) {
        super();
        this.ret = ret;
        this.data = data;
        this.total = total;
    }

    /**
     * 成功，无数据
     *
     * @return
     */
    public static <T> JsonResult<T> succ() {
        return new JsonResult<T>(ResponseCode.SUCC);
    }

    /**
     * 成功，有数据
     *
     * @param data
     * @return
     */
    public static <T> JsonResult<T> succ(T data) {
        return new JsonResult<T>(data, ResponseCode.SUCC);
    }

    /**
     * 成功，有数据，分页
     *
     * @param data
     * @return
     */
    public static <T> JsonResult<T> succ(Integer total, T data) {
        return new JsonResult<T>(ResponseCode.SUCC, data, total);
    }

    /**
     * 失败
     *
     * @param msg
     * @param code
     * @return
     */
    public static <T> JsonResult<T> fail(Integer code, String msg, String traceId) {
        if (code == null) {
            code = ResponseCode.FAIL;
        }
        return new JsonResult<T>(code, msg, traceId);
    }

    /**
     * 失败内容
     *
     * @param msg
     * @param code
     * @return
     */
    public static <T> JsonResult<T> failContent(Integer code, String msg, String content, String traceId) {
        if (code == null) {
            code = ResponseCode.FAIL;
        }
        return new JsonResult<T>(code, msg, content, traceId);
    }

    public Integer getRet() {
        return ret;
    }

    public void setRet(Integer ret) {
        this.ret = ret;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}
